/**
 * Created by shining on 15/5/11.
 */

$(function () {
   $.extend({
      //   提示
      alert: function (type, msg) {
         swal({
            title: "",
            text: msg,
            type: type,
            timer: 1000,
            confirmButtonText: '确定'
         });
      },
      //确认提示
      confirm: function (msg, callback) {
         swal({
            title: "",
            text: msg,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true,
            closeOnCancel: true
         }, function (isConfirm) {
            if (isConfirm) {
               if (!!callback && typeof callback == 'function') {
                  callback();
               }
            }
         });
      },
      //ajax loading
      getAjax: function (data, open, resolve, reject) {
         $.ajax({
            url: data.url,
            method: data.method,
            data: data.data,
            beforeSend: function () {
               $('.scrm-loadding').show();
            }
         }).
            done(function (res) {
               $('.scrm-loadding').hide();
               var msg = $.parseJSON(res),
                  status = {
                     'F': 'error',
                     'S': 'success'
                  };

               if (open) {
                  $.alert(status[msg.status], msg.message);
               }

               if (msg.errcode && msg.errcode === 'logout') {
                  location.reload();
               }

               if (msg.status === 'S' && resolve && typeof(resolve) === 'function') {
                  resolve(msg);
               } else if (msg.status === 'F' && reject && typeof(reject) === 'function') {

                  reject(msg);
               }
            }).
            fail(function () {
               $('.scrm-loadding').hide();
               $.alert('error', '通讯错误');
            });
      }
   });

   $.fn.extend({
      changeAmount: function () {
         var elem = $(this),
            plus = $(this).find('[data-status=plus]'),
            sub = $(this).find('[data-status=sub]'),
            input = $(this).find('[data-status=input]');

         try {
            elem.on('click', '[data-status=plus]', function () {
               renderNumVal('plus', input.val(), input);
            });

            elem.on('click', '[data-status=sub]', function () {
               renderNumVal('sub', input.val(), input);
            });

            elem.on('change', '[data-status=input]', function () {
               renderNumVal('input', input.val(), input);
            });

         } catch (err) {
            console.log(err);
         }

         function renderNumVal(status, initVal, elemVal) {
            var initNum = initVal;
            if (status == 'plus') {
               initNum++;
            } else if (status == 'sub') {
               if (initNum <= 1) {
                  initNum = 1;
                  return false;
               }
               initNum--;
            } else if (status == 'input') {
               if (initNum <= 1) {
                  initNum = 1;
               }
               elemVal.val(initNum);
            }
            elemVal.val(initNum);
         }
      }
   });
   $('body').append('<div class="scrm-loadding">' +
   '<svg width="95" height="20" id="gb-loading">' +
   '<rect width="20" height="20" x="0" y="0" rx="3" ry="3">' +
   '<animate attributeName="width" values="0;20;20;20;0" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="height" values="0;20;20;20;0" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="x" values="10;0;0;0;10" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="y" values="10;0;0;0;10" dur="1000ms" repeatCount="indefinite"/>' +
   '</rect>' +
   '<rect width="20" height="20" x="25" y="0" rx="3" ry="3">' +
   '<animate attributeName="width" values="0;20;20;20;0" begin="200ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="height" values="0;20;20;20;0" begin="200ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="x" values="35;25;25;25;35" begin="200ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="y" values="10;0;0;0;10" begin="200ms" dur="1000ms" repeatCount="indefinite"/>' +
   '</rect>' +
   '<rect width="20" height="20" x="50" y="0" rx="3" ry="3">' +
   '<animate attributeName="width" values="0;20;20;20;0" begin="400ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="height" values="0;20;20;20;0" begin="400ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="x" values="60;50;50;50;60" begin="400ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="y" values="10;0;0;0;10" begin="400ms" dur="1000ms" repeatCount="indefinite"/>' +
   '</rect>' +
   '<rect width="20" height="20" x="75" y="0" rx="3" ry="3">' +
   '<animate attributeName="width" values="0;20;20;20;0" begin="800ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="height" values="0;20;20;20;0" begin="800ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="x" values="85;75;75;75;85" begin="800ms" dur="1000ms" repeatCount="indefinite"/>' +
   '<animate attributeName="y" values="10;0;0;0;10" begin="800ms" dur="1000ms" repeatCount="indefinite"/>' +
   '</rect>' +
   '</svg>' +
   '</div>');
});